<template>
  <div class="edit-user" v-loading="loading">
    <el-card
      style="width: 95%; height: 70px; display: flex; margin-bottom: 20px"
    >
      <el-form :model="form" style="display: flex; width: 100%">
        <el-form-item label="用户名:" label-width="70">
          <el-input
            v-model="form.username"
            placeholder="用户名查询"
            @keyup.enter.native="getTableData"
            @clear="getTableData"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号:" label-width="70">
          <el-input
            v-model="form.phone"
            placeholder="手机号查询"
            @keyup.enter.native="getTableData"
            @clear="getTableData"
            clearable
          />
        </el-form-item>
        <el-form-item label="地址:" label-width="60">
          <el-input
            v-model="form.address"
            placeholder="地址查询"
            @keyup.enter.native="getTableData"
            @clear="getTableData"
            clearable
          />
        </el-form-item>
        <el-form-item label="角色类型:" label-width="90">
          <el-select v-model="form.userType">
            <el-option label="全部" value="" />
            <el-option label="社区业主" value="0" />
            <el-option label="社区管理员" value="1" />
            <el-option label="系统管理员" value="2" />
          </el-select>
        </el-form-item>

        <div style="width: 15px"></div>
        <el-button @click="getTableData" type="primary" :icon="Search"
          >搜索</el-button
        >
      </el-form></el-card
    >

    <el-table :data="tableData" style="width: 95%" border max-height="480">
      <el-table-column prop="iconUrl" label="头像" width="80" align="center">
        <template #default="scope">
          <el-image
            v-if="scope.row.iconUrl"
            style="width: 50px; height: 50px; border-radius: 50px"
            :src="scope.row.iconUrl"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-teleported="true"
            :preview-src-list="[scope.row.iconUrl]"
            fit="cover"
          /> </template
      ></el-table-column>
      <el-table-column prop="username" label="用户名" align="center" />
      <el-table-column
        prop="nickName"
        label="用户昵称"
        align="center"
        width="100"
      />
      <el-table-column
        prop="phone"
        label="电话号码"
        width="120"
        align="center"
      />
      <el-table-column
        prop="keepingPet"
        label="是否养宠"
        align="center"
        width="100"
        ><template #default="scope">
          <div v-if="scope.row.keepingPet">是</div>
          <div v-else>否</div>
        </template></el-table-column
      >
      <el-table-column prop="address" label="地址" align="center" width="180" />
      <el-table-column
        prop="userScore"
        label="用户积分"
        align="center"
        width="100"
      />
      <el-table-column
        prop="userType"
        label="角色类型"
        align="center"
        width="100"
      >
        <template #default="scope">
          <div v-if="scope.row.userType === '0'">社区业主</div>
          <div v-else-if="scope.row.userType === '1'">社区管理员</div>
          <div v-else>系统管理员</div>
        </template></el-table-column
      >
      <el-table-column
        prop="status"
        label="账户状态"
        align="center"
        width="100"
      >
        <template #default="scope">
          <div v-if="scope.row.status === '0'">正常</div>
          <div v-else>封禁</div>
        </template></el-table-column
      >
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        width="180"
      />
      <el-table-column
        prop="updateTime"
        label="更新时间"
        align="center"
        width="180"
      />
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">
            编辑角色</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 95%; margin-top: 10px">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
  <EditDialog ref="editDialogRef" @getTableData="getTableData" />
</template>

<script setup lang="ts">
import { getListUserInfo } from "@/api/user";
import { ref, onMounted, reactive } from "vue";
import { Search } from "@element-plus/icons-vue";
import EditDialog from "./edit-dialog.vue";
const editDialogRef = ref();
const tableData = ref<any>([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const form = reactive({
  username: "",
  phone: "",
  userType: "",
  address: "",
});
const handleSizeChange = () => {
  getTableData();
};
const handleCurrentChange = () => {
  getTableData();
};
const handleEdit = (data: any) => {
  editDialogRef.value.showDialog(data);
};
const loading = ref(false);
const getTableData = async () => {
  loading.value = true;
  await getListUserInfo({
    page: currentPage.value,
    pageSize: pageSize.value,
    phone: form.phone,
    username: form.username,
    userType: form.userType,
    address: form.address,
  }).then((res: any) => {
    tableData.value = res.data;
    total.value = +res.total;
  });
  loading.value = false;
};
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="less">
.edit-user {
  color: black;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  padding-top: 20px;
}
</style>
